package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.device.protocol.response.BxyAlarm;
import org.apache.commons.lang.math.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 波轮洗衣机
 * Created by Huqin on 2020/1/15
 */
public class BxyAlarmMessageDecoder extends ByteToMessageDecoder<BxyAlarm>{

    private List<FieldDecoder<BxyAlarm>> fieldDecoderList;

    public BxyAlarmMessageDecoder() {
        fieldDecoderList = new ArrayList<>();
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(8, 1, this.alarm4Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(7, 1, this.alarm5Map()));
        fieldDecoderList.add(new BitSwitchFieldDecoder<>(5, 1, this.alarm7Map()));
    }

    @Override
    protected List<FieldDecoder<BxyAlarm>> fieldDecoderList() {
        return fieldDecoderList;
    }

    @Override
    protected BxyAlarm prepare() {
        return new BxyAlarm();
    }

    @Override
    protected boolean preProcessBeforeDecode(byte[] data, BxyAlarm obj) {
        for (byte byt : data) {
            if (NumberUtils.BYTE_ZERO != byt) {
                return false;
            }
        }
        obj.setAlarmCancel(true);
        return true;
    }

    public Map<Integer, BiConsumer<BxyAlarm, Boolean>> alarm4Map() {
        Map<Integer, BiConsumer<BxyAlarm, Boolean>> map = new HashMap<>();
        map.put(4, BxyAlarm::setWaterLevelSensorErr);
        map.put(5, BxyAlarm::setInWaterErr);
        map.put(6, BxyAlarm::setDrainErr);
        map.put(7,BxyAlarm::setOverflowAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<BxyAlarm, Boolean>> alarm5Map() {
        Map<Integer, BiConsumer<BxyAlarm, Boolean>> map = new HashMap<>();
        map.put(4,BxyAlarm::setSpinUnbalanceAlarm);
        return map;
    }

    public Map<Integer, BiConsumer<BxyAlarm, Boolean>> alarm7Map() {
        Map<Integer, BiConsumer<BxyAlarm, Boolean>> map = new HashMap<>();
        map.put(0, BxyAlarm::setDoorErr);
        return map;
    }
}
